package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.service.BankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

/**
 * @author 22096
 * @description 针对表【bank_account(银行账户表)】的数据库操作Service实现
 * @createDate 2024-12-02 21:28:13
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount>
        implements BankAccountService {

    /**
     * 新增或更新银行账号信息接口
     *
     * @param bankAccount
     */
    @Override
    public void addOrUpdateBankAccount(BankAccount bankAccount) {
        bankAccount.setUserId(UserContext.currentUserId());
        LambdaQueryWrapper<BankAccount> queryWrapper = Wrappers.<BankAccount>lambdaQuery().eq(BankAccount::getAccount, bankAccount.getAccount()).eq(BankAccount::getUserId, UserContext.currentUserId()).eq(BankAccount::getType, bankAccount.getType());
        BankAccount account = this.getOne(queryWrapper);
        if (ObjectUtil.isNotNull(account)) {
            boolean update = this.update(bankAccount, null);
            if (!update) {
                throw new DBException("操作失败");
            }
        } else {
            boolean save = this.save(bankAccount);
            if (!save) {
                throw new DBException("操作失败");
            }
        }
    }

    /**
     * 获取当前用户银行账号接口
     * @return
     */
    @Override
    public BankAccount getBankAccount() {
        LambdaQueryWrapper<BankAccount> queryWrapper = Wrappers.<BankAccount>lambdaQuery().eq(BankAccount::getUserId, UserContext.currentUserId());
        BankAccount account = this.getOne(queryWrapper);
        if (ObjectUtil.isNull(account)){
            throw new CommonException("当然用户不存在账户");
        }
        return account;
    }
}




